#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
工作人员数据模型
"""

from database import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash

class Staff(db.Model):
    __tablename__ = 'staff'
    
    id = db.Column(db.Integer, primary_key=True)
    staff_no = db.Column(db.String(20), unique=True, nullable=False, comment='员工编号')
    username = db.Column(db.String(50), unique=True, nullable=False, comment='登录用户名')
    password_hash = db.Column(db.String(255), nullable=False, comment='密码哈希')
    name = db.Column(db.String(100), nullable=False, comment='真实姓名')
    phone = db.Column(db.String(20), nullable=True, comment='手机号')
    
    # 微信绑定信息
    openid = db.Column(db.String(100), unique=True, nullable=True, comment='微信OpenID')
    unionid = db.Column(db.String(100), unique=True, nullable=True, comment='微信UnionID')
    
    # 职位信息
    position = db.Column(db.String(50), nullable=False, comment='职位：manager店长，waiter服务员，cleaner清洁员，technician技术员')
    department = db.Column(db.String(50), nullable=True, comment='部门')
    
    # 工作状态
    status = db.Column(db.String(20), default='active', comment='状态：active在职，leave离职，suspend停职')
    work_status = db.Column(db.String(20), default='off', comment='工作状态：on上班，off下班，busy忙碌')
    
    # 权限信息
    permissions = db.Column(db.Text, nullable=True, comment='权限列表（JSON格式）')
    
    # 系统字段
    hire_date = db.Column(db.DateTime, nullable=True, comment='入职时间')
    created_at = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    last_login_at = db.Column(db.DateTime, nullable=True, comment='最后登录时间')
    
    # 关联关系
    tasks = db.relationship('Task', backref='staff', lazy='dynamic')
    
    def __init__(self, **kwargs):
        super(Staff, self).__init__(**kwargs)
        if 'password' in kwargs:
            self.password = kwargs['password']
    
    @property
    def password(self):
        raise AttributeError('密码不可读')
    
    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)
    
    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)
    
    def to_dict(self):
        return {
            'id': self.id,
            'staff_no': self.staff_no,
            'username': self.username,
            'name': self.name,
            'phone': self.phone,
            'openid': self.openid,
            'position': self.position,
            'department': self.department,
            'status': self.status,
            'work_status': self.work_status,
            'permissions': self.permissions,
            'hire_date': self.hire_date.isoformat() if self.hire_date else None,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'last_login_at': self.last_login_at.isoformat() if self.last_login_at else None
        }
    
    def __repr__(self):
        return f'<Staff {self.name}({self.staff_no})>'